package cn.byapps.attendance.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import cn.byapps.attendance.entity.Ad;
import cn.byapps.attendance.service.AdService;
import cn.byapps.attendance.util.DataTablesResult;
import core.controller.BaseController;
import core.utils.Md5Util;

@Controller
@RequestMapping("/wechat/ad")
public class AdController extends BaseController {

	@Resource
	private AdService adService;
	@Value("${server.upload}")
	private String uploadPath;

	@RequestMapping(value = { "index", "/", "" })
	public String index() {
		return "/wechat/ad/index";
	}

	@RequestMapping(value = { "getData" }, method = RequestMethod.POST)
	public void getData(Integer draw, Long start, Long length, HttpServletResponse response) {
		int pos = 0;
		int rows = 10;
		if (start != null) {
			pos = start.intValue();
		}
		if (length != null) {
			rows = length.intValue();
		}
		DataTablesResult<Ad> lists = adService.getData(draw, pos, rows);
		if(lists == null) {
			lists = new DataTablesResult<Ad>();
		}
		try {
			this.writeJSON(response, lists);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "wechat/ad/add";
	}

	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(String id, Model model) {
		Ad ad = adService.get(id);
		model.addAttribute("item", ad);
		return "wechat/ad/update";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(String id, String name) {
		Ad ad = new Ad();
		ad.setId(id);
		ad.setName(name);
		adService.updateName(ad);
		return "redirect:index";
	}

	@RequestMapping(value = "use", method = RequestMethod.GET)
	public String update(String id) {
		adService.updateFlag(id);
		return "redirect:index";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(MultipartFile file, String name, HttpServletRequest request, Model model) throws IOException {
		// 判断文件是否为空
		// 如果不等于空
		if (!file.isEmpty()) {
			// 设置存储文件的路径 获取 服务器中当前WebRoot的物理路径
			String path = request.getSession().getServletContext().getRealPath(uploadPath);
			// 获取文件名字
			String fileName = file.getOriginalFilename();
			Ad ad = new Ad();
			ad.setName(name);
			ad.setId(UUID.randomUUID().toString());
			String filename = ad.getId() + fileName.substring(fileName.lastIndexOf("."));
			ad.setPath("/" + uploadPath + "/" + filename);
			File destFile = new File(path, filename);
			FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);
			ad.setMd5(Md5Util.md5(destFile, false));
			adService.add(ad);
			// 将文件上传到目录
		}
		return "redirect:index";
	}
}
